#!/usr/bin/php -q
<?php
// 取得相簿列表
require_once("foto.inc.php");



if (!file_exists($otpw_cache)) {
	echo "[add_ab] no otpw cache, abort\n";
	exit;
}

$otpw=getotpwcache();

if (empty($argv[1] ) ) {
	printf("Usage: %s title [desc=] [private|public|passwd=] [no_cp] [no_riview]\n",
		$argv[0]);
	// last match if public and passwd exists
	exit;
}
$title=$argv[1];
$vis = 1;
$desc = "";
$pass = "";
$is_review='Y';
$is_cp='Y';
for($i=1;$i<$argc;$i++) {
	if (strstr($argv[$i] , "=")) {
		list($key, $val) = explode("=",$argv[$i]);
		switch($key) {
			case 'desc':
				$desc = $val; break;
			case 'passwd':
				if (empty($val) || !preg_match("/[\w]+/",$val)) {
					echo "invaild password\n";
					exit;
				}
				$pass = $val;
				$vis=2; break;
		}
	} else {
		switch($argv[$i]){
			case 'private':
				$pass = "";
				$vis=0; break;
			case 'public':
				$pass = "";
				$vis=1; break;
			case 'no_cp':
				$is_cp = 'N'; break;
			case 'no_review':
				$is_review = 'N'; break;
				
		}
	}
}

if (add_ab($otpw, $title, $desc, $vis, $pass, $is_review, $is_cp )) {
	// 成功新增
	$a = list_ab($otpw);
	foreach($a as $aid => $atitle) {
		$album_id = $aid;
		$album_title = $atitle;
		break;
	}
	printf("[add_ab] create album %d %s successful\n",$album_id,$album_title);
	echo "setenv ab_id $album_id\n";
}

?>
